package com.netflexitysolutions.amazonws.ec2.internal.operations;

import java.util.List;

import com.amazonaws.ec2.doc._2009_04_04.LaunchPermissionListType;
import com.amazonaws.ec2.doc._2009_04_04.LaunchPermissionOperationType;
import com.amazonaws.ec2.doc._2009_04_04.ModifyImageAttributeType;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.LaunchPermission;
import com.netflexitysolutions.amazonws.ec2.internal.ModelUtil;

public class ModifyLaunchPermissionsOperation extends SingleObjectOperationExecutor<ModifyImageAttributeType, Boolean> {
	
	public enum Action {
		ADD, REMOVE
	}
	
	private List<LaunchPermission> permissions;
	
	private Action action;
	
	public ModifyLaunchPermissionsOperation(EC2 ec2, String imageId, List<LaunchPermission> permissions, Action action) {
		super(ec2, imageId);
		this.action = action;
		this.permissions = permissions;
	}

	@Override
	protected Boolean call(ModifyImageAttributeType request) {
		request.setImageId(objectId);
		LaunchPermissionOperationType operation = new LaunchPermissionOperationType();
		LaunchPermissionListType xmlPermisions = ModelUtil.toXMLType(permissions);
		switch (action) {
		case ADD:
			operation.setAdd(xmlPermisions);
			break;
			
		case REMOVE:
			operation.setRemove(xmlPermisions);
			break;
		}
		return getEC2().getService().modifyImageAttribute(request).isReturn();
	}

}
